package cn.virens.web.common;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;

import cn.virens.common.RequestUtil;
import cn.virens.database.enums.log.LogLevel;
import cn.virens.database.enums.log.LogType;
import cn.virens.database.mysql.model.sys.SysLog;
import cn.virens.database.mysql.model.sys.SysRole;
import cn.virens.database.mysql.model.sys.SysUser;
import cn.virens.database.service.sys.SysLogService;
import cn.virens.web.components.shiro.simple.simple.SimpleUserFilter;

/**
 * 通用控制器-后台
 * 
 * @文件   :BaseJqGridController.java
 * @作者   :cn.loioi
 * @创建时间 :2016年10月13日 下午1:10:43
 */
public class BaseUserController extends BaseJSONController {
	protected final static String LOGIN_USER = SimpleUserFilter.LOGIN_USER;
	protected final static String LOGIN_ROLE = SimpleUserFilter.LOGIN_ROLE;

	protected String _identity;

	protected @Autowired SysLogService mSysLogService;

	protected BaseUserController() {
		super();
	}

	/**
	 * @param baseTemplateUrl 模板根路径
	 */
	protected BaseUserController(String baseTemplateUrl) {
		super(baseTemplateUrl);
	}

	/**
	 * @param baseTemplateUrl 模板根路径
	 * @param identity        顶层标示
	 */
	protected BaseUserController(String baseTemplateUrl, String identity) {
		super(baseTemplateUrl);
		this._identity = identity;
	}

	/**
	 * 获取 当前登录的用户
	 * 
	 * @param  request
	 * @return
	 */
	protected final SysUser getUser(HttpServletRequest request) {
		return getAttribute(request, LOGIN_USER, SysUser.class);
	}

	/**
	 * 获取 当前登录用户的角色
	 * 
	 * @param  request
	 * @return
	 */
	protected final SysRole getRole(HttpServletRequest request) {
		return getAttribute(request, LOGIN_ROLE, SysRole.class);
	}

	/**
	 * 获取Subject
	 * 
	 * @return
	 */
	protected final Subject getSubject(HttpServletRequest request) {
		return SecurityUtils.getSubject();
	}

	/**
	 * 获取 当前登录用户的账号
	 * 
	 * @param  request
	 * @return
	 */
	protected final String getAccount(HttpServletRequest request) {
		return String.valueOf(getSubject(request).getPrincipal());
	}

	/**
	 * 检查角色
	 * 
	 * @param roleIdentifiers
	 */
	protected final void checkRoles(HttpServletRequest request, String... roleIdentifiers) {
		getSubject(request).checkRoles(roleIdentifiers);
	}

	/**
	 * 检查权限
	 * 
	 * @param permission
	 */
	protected final void checkPermission(HttpServletRequest request, String... permission) {
		getSubject(request).checkPermissions(permission);
	}

	/**
	 * 记录日志
	 * 
	 * @param request
	 * @param content
	 */
	protected void log(HttpServletRequest request, String content) {
		SysLog record = new SysLog();
		record.setContent("请求地址：" + request.getRequestURI() + "->" + content);
		record.setHost(RequestUtil.getRemoteAddr(request));
		record.setLevel(LogLevel.TRACE.getKey());
		record.setType(LogType.USER.getKey());
		record.setUser(getAccount(request));

		mSysLogService.insert(record);
	}

	/**
	 * 记录请求参数日志
	 * 
	 * @param request
	 * @param title
	 */
	protected void logParam(HttpServletRequest request, String title) {
		log(request, title + "请求参数:" + JSON.toJSONString(request.getParameterMap()));
	}
}
